<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use OSS\OssClient;
use OSS\Core\OssException;

class OssServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        $this->app->singleton(OssClient::class, function ($app) {
            $config = $app['config']['oss'];

            if (empty($config['access_key_id']) || empty($config['access_key_secret']) || empty($config['endpoint']) || empty($config['bucket'])) {
                throw new \InvalidArgumentException('OSS configuration is incomplete.');
            }

            try {
                return new OssClient(
                    $config['access_key_id'],
                    $config['access_key_secret'],
                    $config['endpoint']
                );
            } catch (OssException $e) {
                // 可以记录日志或者抛出更具体的异常
                throw new \RuntimeException('Failed to create OssClient: ' . $e->getMessage());
            }
        });
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        //
    }
}
